<?php
/*
 * (C) Copyright 2013 Torsten Gorath (http://www.blauesdach.de/).
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 * Contributors:
 *     Torsten Gorath
 */
require_once $basedir."/inc/vertraege.php";
$vertrag = new Vertraege();
if(isset($_REQUEST['id'])) {
	$vertrag = new Vertraege($_REQUEST['id']);
}
if(isset($_REQUEST['save'])) {
	$vertrag->setKindId($_REQUEST['kind_id']);
	$vertrag->setAufnahmegebuehr($_REQUEST['aufnahmegebuehr']);
	$vertrag->setAufnahmegebuehrEingezogenAm($_REQUEST['aufnahmegebuehr_eingezogen_am']);
	$vertrag->setAusgeschiedenAm($_REQUEST['ausgeschieden_am']);
	$vertrag->setBetreuungVon($_REQUEST['betreuung_von']);
	$vertrag->setBetreuungBis($_REQUEST['betreuung_bis']);
	$vertrag->setBaugeld($_REQUEST['baugeld']);
	$vertrag->setBaugeldEingezogenAm($_REQUEST['baugeld_eingezogen_am']);
	$vertrag->setDarlehen($_REQUEST['darlehen']);
	$vertrag->setDarlehenEingezogenAm($_REQUEST['darlehen_eingezogen_am']);
	$vertrag->setDarlehenZurueckAm($_REQUEST['darlehen_zurueck_am']);
	$vertrag->setBankId($_REQUEST['bank_id']);
	$vertrag->setHygienebeitrag($_REQUEST['hygienebeitrag']);
	$vertrag->setMonatsbeitrag($_REQUEST['monatsbeitrag']);
	$vertrag->setSozialamt($_REQUEST['sozialamt']);
    $vertrag->setZuschussStadt($_REQUEST['zuschuss_stadt']);
	$vertrag->setSpende($_REQUEST['spende']);
	$vertrag->setVertragAb($_REQUEST['vertrag_ab']);
	$vertrag->save();
}
$kind = $vertrag->getKind();
$bank = $vertrag->getBank();
if($action=='edit') {
	$action = 'add';
}
if($action=='add') {
	$form_display = "block";
} else {
	$form_display = "none";
}
?>
<script language="JavaScript" type="text/javascript">
    $(function() {
	    $('#nurAktuelle').click(function(event) {
	    	var params = [
	    		'page=<?php echo $menuid; ?>',
	    		'onlyCurrent=' + $('#nurAktuelle').is(':checked')
	    	];
	    	window.location.href = "http://" + window.location.host + window.location.pathname + "?" + params.join('&');
	    });
        $( "#kind_id" ).autocomplete({
            source: function(request, response) {
            	var data = {
            		'value': request.term,
            		'page': 'stammdaten_kinder',
            		'action': 'search'
            	};
            	$.ajax({
            		'url': "json.php",
            		'data': data,
            		'type':'post',
            		'dataType': 'json',
            		'success': function(data) {
            			response(data.items);
            		}
            	})
            },
            minLength: 2,
            select: function(event, ui) {
            	$('input[name=kind_id]').attr('value', ui.item.id);
            }
        })
        .data("ui-autocomplete")._renderItem = function(ul, item) {
        	item.label = "<a>" + item.vorname + " " + item.nachname + "</a>";
        	item.value = item.vorname + " " + item.nachname;
        	return $("<li></li>")
        		.data("item.autocomplete", item)
        		.append(item.label)
        		.appendTo(ul);
        };
        $( "#bank_id" ).autocomplete({
            source: function(request, response) {
            	var data = {
            		'value': request.term,
            		'page': 'stammdaten_bank',
            		'action': 'search'
            	};
            	$.ajax({
            		'url': "json.php",
            		'data': data,
            		'type':'post',
            		'dataType': 'json',
            		'success': function(data) {
            			response(data.items);
            		}
            	})
            },
            minLength: 2,
            select: function(event, ui) {
            	$('input[name=bank_id]').attr('value', ui.item.id);
            }
        })
        .data("ui-autocomplete")._renderItem = function(ul, item) {
        	item.label = "<a>" + item.name + "<br />"
        			+ "BLZ: " + item.blz + "<br />"
        			+ "Kontonr: " + item.kontonummer + "</a>";
        	item.value = item.name;
        	return $("<li></li>")
        		.data("item.autocomplete", item)
        		.append(item.label)
        		.appendTo(ul);
        };
    });
</script>
<form id='stammdaten_vertrag_add_form' method="post" action="/index.php" style='display:<?php echo $form_display; ?>'>
	<input type="hidden" name="page" value="<?php echo $menuid;?>" />
	<input type="hidden" name="id" value="<?php echo $vertrag->getId();?>" />
    <div>
    	<div class='inline-block label'>Kind:</div>
    	<div class='inline-block'>
	        <input name="kind_id" type="hidden" value="<?php echo $vertrag->getKindId(); ?>" />
	        <input type="text" name="kind" id='kind_id' maxlength="101" value="<?php echo ($kind->getVorname()!=""?$kind->getVorname()." ":"").$kind->getNachname();?>" />
    	</div>
    </div>
    <div>
    	<div class='inline-block label'>Bankverbindung:</div>
    	<div class='inline-block'>
	        <input name="bank_id" type="hidden" value="<?php echo $vertrag->getBankId(); ?>" />
	        <input type="text" name="bank" id='bank_id' maxlength="100" value="<?php echo $bank->getName();?>" />
    	</div>
    </div>
    <div>
    	<div class='inline-block label'>Vertrag ab:</div>
    	<div class='inline-block'>
	        <input type="date" name="vertrag_ab" maxlength="10" value="<?php echo $vertrag->getVertragAb();?>" />
    	</div>
    </div>
    <div>
    	<div class='inline-block label'>Betreuung von:</div>
    	<div class='inline-block'>
	        <input type="date" name="betreuung_von" maxlength="8" value="<?php echo $vertrag->getBetreuungVon();?>" />
    	</div>
    </div>
    <div>
    	<div class='inline-block label'>Betreuung bis:</div>
    	<div class='inline-block'>
	        <input type="date" name="betreuung_bis" maxlength="8" value="<?php echo $vertrag->getBetreuungBis();?>" />
    	</div>
    </div>
    <div>
    	<div class='inline-block label'>Darlehen:</div>
    	<div class='inline-block'>
	        <input type="number" name="darlehen" maxlength="10" value="<?php echo $vertrag->getDarlehen();?>" />
    	</div>
    </div>
    <div>
    	<div class='inline-block label'>Darlehen eingezogen am:</div>
    	<div class='inline-block'>
	        <input type="date" name="darlehen_eingezogen_am" maxlength="10" value="<?php echo $vertrag->getDarlehenEingezogenAm();?>" />
    	</div>
    </div>
    <div>
    	<div class='inline-block label'>Darlehen zur&uuml;ck am:</div>
    	<div class='inline-block'>
	        <input type="date" name="darlehen_zurueck_am" maxlength="10" value="<?php echo $vertrag->getDarlehenZurueckAm();?>" />
    	</div>
    </div>
    <div>
    	<div class='inline-block label'>Aufnahmegeb&uuml;hr:</div>
    	<div class='inline-block'>
	        <input type="number" name="aufnahmegebuehr" maxlength="10" value="<?php echo $vertrag->getAufnahmebgebuehr();?>" />
    	</div>
    </div>
    <div>
    	<div class='inline-block label'>Aufnahmegeb&uuml;hr eingezogen am:</div>
    	<div class='inline-block'>
	        <input type="date" name="aufnahmegebuehr_eingezogen_am" maxlength="10" value="<?php echo $vertrag->getAufnahmegebuehrEingezogenAm();?>" />
    	</div>
    </div>
    <div>
    	<div class='inline-block label'>Baugeld:</div>
    	<div class='inline-block'>
	        <input type="number" name="baugeld" maxlength="10" value="<?php echo $vertrag->getBaugeld();?>" />
    	</div>
    </div>
    <div>
    	<div class='inline-block label'>Baugeld eingezogen am:</div>
    	<div class='inline-block'>
	        <input type="date" name="baugeld_eingezogen_am" maxlength="10" value="<?php echo $vertrag->getBaugeldEingezogenAm();?>" />
    	</div>
    </div>
    <div>
    	<div class='inline-block label'>Monatsbeitrag:</div>
    	<div class='inline-block'>
	        <input type="number" name="monatsbeitrag" maxlength="10" value="<?php echo $vertrag->getMonatsbeitrag();?>" />
    	</div>
    </div>
    <div>
    	<div class='inline-block label'>Sozialamt:</div>
    	<div class='inline-block'>
	        <input type="checkbox" name="sozialamt" value="<?php echo ($vertrag->getSozialamt()==true?"1":"0");?>" <?php echo ($vertrag->getSozialamt()==true?"checked":""); ?> />
	        <input type="number" name='zuschuss_stadt' maxlength="10" value="<?php echo $vertrag->getZuschussStadt();?>" />
    	</div>
    </div>
    <div>
    	<div class='inline-block label'>Spende:</div>
    	<div class='inline-block'>
	        <input type="number" name="spende" maxlength="10" value="<?php echo $vertrag->getSpende();?>" />
    	</div>
    </div>
    <div>
    	<div class='inline-block label'>Hygienebeitrag:</div>
    	<div class='inline-block'>
	        <input type="number" name="hygienebeitrag" maxlength="10" value="<?php echo $vertrag->getHygienebeitrag();?>" />
    	</div>
    </div>
    <div>
    	<div class='inline-block label'>Ausgeschieden am:</div>
    	<div class='inline-block'>
	        <input type="date" name="ausgeschieden_am" maxlength="10" value="<?php echo $vertrag->getAusgeschiedenAm();?>" />
    	</div>
    </div>
    <div>
        <button type="submit" name="save">Speichern</button>
        <button type="submit" name="reset">Abbrechen</button>
    </div>
</form>
<?php
if($action!="add") {
	$onlyCurrent=true;
	if(isset($_REQUEST['onlyCurrent'])) {
		$onlyCurrent = $_REQUEST['onlyCurrent']=='true'?true:false;
	}
	$vertraege = Vertraege::getAll($onlyCurrent);
?>
<div>
    <a href='?page=<?php echo $menuid; ?>&action=add'>Neuer Vertrag</a> ||
    <input type='checkbox' value='' name='nurAktuelle' id='nurAktuelle' <?php echo $onlyCurrent?'checked':''; ?>></input> Nur Aktuelle
</div>
<div>
    <table border="1" bordercolor="black" cellpadding="0" cellspacing="0" >
        <tr>
            <th>Kind</th>
            <th>Bankverbindung</th>
            <th>Ab</th>
            <th>Bis</th>
            <th>Betreuung</th>
            <th colspan='2'>Darlehen</th>
            <th colspan='2'>Aufnahmegeb&uuml;hr</th>
            <th colspan="2">Baugeld</th>
            <th>Monatsbeitrag</th>
            <th>Sozialamt</th>
            <th>Spende</th>
            <th>Hygienebeitrag</th>
            <th></th>
        </tr>
<?php
if($vertraege!=NULL) {
	for($i=0;$i<count($vertraege);$i++) {
		$vertrag = $vertraege[$i];
		$kind = $vertrag->getKind();
        $gruppe = $kind->getGruppenzuordnung();
		$bank = $vertrag->getBank();
        $style = "";
        if($kind->isSchulkind()) {
            $style = " class='bg-green'";
        }
?>
		<tr<? echo $style; ?>>
			<td><?php echo $kind->getVorname()." ".$kind->getNachname();?><br /><? echo $kind->getGeburtsdatum(); ?></td>
			<td><?php echo $bank->getName();?></td>
			<td><?php echo $vertrag->getVertragAb();?></td>
			<td><?php echo $vertrag->getAusgeschiedenAm();?></td>
			<td><?php echo $vertrag->getBetreuungVon()." - ".$vertrag->getBetreuungBis(); ?></td>
			<td class='right'><?php echo $vertrag->getDarlehen()." &euro;";?></td>
			<td><?php echo $vertrag->getDarlehenEingezogenAm();?></td>
			<td class='right'><?php echo $vertrag->getAufnahmebgebuehr()!=0?$vertrag->getAufnahmebgebuehr()." &euro;":"";?></td>
			<td><?php echo $vertrag->getAufnahmegebuehrEingezogenAm();?></td>
			<td class='right'><?php echo $vertrag->getBaugeld()." &euro;"; ?></td>
			<td><?php echo $vertrag->getBaugeldEingezogenAm(); ?></td>
			<td class='right'><?php echo $vertrag->getMonatsbeitrag()." &euro;";?></td>
			<td class='center'><?php echo $vertrag->getSozialamt()==1?"X":"";?> <?php echo $vertrag->getZuschussStadt(); ?></td>
			<td class='right'><?php echo $vertrag->getSpende()!=0?$vertrag->getSpende()." &euro;":"";?></td>
			<td class='right'><?php echo $vertrag->getHygienebeitrag()!=0?$vertrag->getHygienebeitrag()." &euro;":"";?></td>
			<td><a href="?page=<?php echo $menuid; ?>&action=edit&id=<?php echo $vertrag->getId();?>">edit</a></td>
		</tr>
<?php
	}
}
?>
    </table>
</div>
<?php    
}
?>